/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package SystemTray;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;

import GuiTools.GuiTools;
import Mainframe.Mainframe;
import Settings.*;
import Storage.*;

/**
 * Builds SystemTray functions.
 * 
 * @author Christoph Schnack
 *
 */
public class TribuitSystemTray {
	Storage MAINVOC;
	Settings SET;
	Mainframe MAINFRAME;
	Language LANG;
	SystemTray tray;
	TribuitSystemTrayMenu popup;
	
	public TribuitSystemTray(Storage mv, Settings set, Mainframe mf){
		MAINVOC=mv;
		MAINFRAME=mf;
		SET=set;
		LANG = SET.getLanguage();
		tray = SystemTray.getSystemTray();
		popup=new TribuitSystemTrayMenu(MAINVOC,SET,MAINFRAME,tray);
	}
	
	public void initSystemTray(){
    	TrayIcon trayIcon = new TrayIcon(GuiTools.getTribuitIcon(),"Tribuit",popup);
    	trayIcon.setImageAutoSize(true);
    	trayIcon.addActionListener(new DefaultListener());
    	try {
            tray.add(trayIcon);
        } catch (AWTException e) {
           e.printStackTrace();
        }
	}
	
	class DefaultListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			MAINFRAME.setVisible(true);
			if(MAINFRAME.getState()==JFrame.ICONIFIED) MAINFRAME.setState(JFrame.NORMAL);
		}
	}
	
	
	public void updateTray(){
		if(SET.getAddToSystemTray()){
			if(tray.getTrayIcons()==null || tray.getTrayIcons().length<1) this.initSystemTray();
			else{
				popup.setVocabularyMenus();
				popup.updateLanguage();
			}
		}
		else{
			if(tray.getTrayIcons().length>=1) tray.remove(tray.getTrayIcons()[0]);
		}
	}
}
